﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using UIDATALayer;
using UserData;
namespace VisionBaseLib
{
    public partial class DefineVisionImage : UserControl,Interface_UserControl
    {
        string  str_CameraName = "";
        public string 相机名称
        {
            set { str_CameraName = value; }
            get { return str_CameraName; }
        }
        public DefineVisionImage()
        {
            InitializeComponent();
        }
        private UIDATALayer.UIDataAction<DefineVisionData> acHYVisionData = null;

        private void VisionImage_Load(object sender, EventArgs e)
        {

        }
        public void Init()
        {
            VisionBaseMgr.GetInstance().OnTest_ShowImage += ShowVisionImage;
        }
        object o_ShowVisionImage = new object();
        PictureBox picture = new PictureBox();
        public void ShowVisionImage(string stationName, Bitmap image_Ori, Bitmap image_Draw)
        {
            if(acHYVisionData==null)
            {
                UIDATALayerHelper.RegistAction<DefineVisionData>(nameof(DefineVisionData), ShowVisionImage).Start();
                acHYVisionData = UIDATALayer.UIDATALayerHelper.GetRegistAction<DefineVisionData>(nameof(DefineVisionData));
                acHYVisionData.SetQueueNum(2);
            }
            if (相机名称 == str_CameraName)
            {
                DefineVisionData data = new DefineVisionData();
                data.原始图像 = image_Ori;
                data.图形图像 = image_Draw;
                data.相机名称 = stationName;
                acHYVisionData.SetData(data);
            }
        }
        public void ShowVisionImage(DefineVisionData data)
        {

            if (str_CameraName != data.相机名称)
                return;

            if (pic_Image.InvokeRequired)
            {
                pic_Image.BeginInvoke(new Action(() => ShowVisionImage(data)));
            }
            else
            {
                try
                {
                    lock (VisionBaseMgr.GetInstance().o_ShowVisionImage)
                    {
                        if (null != data.图形图像)
                        {
                            VisionBaseMgr.GetInstance().Function_SetImage(相机名称, (Bitmap)data.原始图像.Clone(), (Bitmap)data.图形图像.Clone());//解决内存不断增加问题
                            pic_Image.Image?.Dispose();
                            pic_Image.Image = (Bitmap)data.图形图像.Clone();//解决切换界面时，刷新失败问题
                            data.图形图像.Dispose();
                            GC.Collect();
                        }

                    }
                }
                catch (Exception ex)
                {

                }
            }
        }

        private void pic_Image_DoubleClick(object sender, EventArgs e)
        {
            Form_VisionImage form_Vision = new Form_VisionImage();
            form_Vision.Text = this.Name;
            form_Vision.TopLevel = true;
            form_Vision.TopMost = true;
            form_Vision.Show();
            picture = form_Vision.pic_Image;
        }
        
    }
}
